﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace NKD07.Classes
{
    public class Basket
    {
        private DataTable data;
        private int quantity = 0;
       // private string primarykey = "";
       // private Int64 total = 0;

        public Basket()
        {
            data = new DataTable();
            data.Columns.Add("STT", typeof(int));
            data.Columns.Add("ID", typeof(int));
            data.Columns.Add("DisplayName", typeof(string));
            data.Columns.Add("Quantity", typeof(int));
            data.Columns.Add("Price", typeof(long));
            data.PrimaryKey = new DataColumn[] { this.data.Columns["ID"] };
        }

        #region Method
        private int kiemtra(int id,int quanti)
        {
            int temp = 0;
            foreach (DataRow dong in data.Rows)
            {
                if (int.Parse(dong["ID"].ToString()) == id)
                    temp = int.Parse(dong["Quantity"].ToString());
                else
                    temp = 0;
            }
            return temp;
        }
        public bool Add(int id,int quanti)
        {
            try
            {
                //if (Exits(id))
                //{
                //    DataRow dong = data.Rows.Find(id);
                //    if (dong != null)
                //    {
                //        dong["Quantity"] = 2;
                //        return true;
                //    }
                //    else
                //    {
                //        return false;
                //    }
                //}
                //else
                //{         
                if (kiemtra(id, quanti) != 0)
                {
                    return false;
                }
                else
                {
                    Classes.product product = new Classes.product();
                    DataTable products = product.GetProductByID(id);
                    foreach (DataRow row in products.Rows)
                    {
                        DataRow newrow = this.data.NewRow();
                        newrow["STT"] = newrow["STT"] = this.quantity + 1; ;
                        newrow["ID"] = id;
                        newrow["Quantity"] = kiemtra(id, quanti) +1;
                        newrow["DisplayName"] = row["name"].ToString();
                        newrow["Price"] = row["priceSell"].ToString();
                        this.data.Rows.Add(newrow);
                        this.quantity++;
                    }
                    return true;
                }
            }
            catch { return false; }
        }
        private void updateStt()
        {
            int dem = 1;
            foreach (DataRow dong in data.Rows)
            {
                dong["STT"] = dem;
                dem++;
            }
        }
        public bool UpdateQuantity(int id, int quan)
        {
            DataRow dong = this.data.Rows.Find(id);
            if (dong != null)
            {
                dong[3] = quan;
                this.data.AcceptChanges();
                return true;
            }
            return false;
        }
        public bool Remove(int id)
        {
            DataRow dong = data.Rows.Find(id);
            if (dong != null)
            {
                dong.Delete();
                this.quantity--;
                data.AcceptChanges();
                return true;
            }
            return false;
        }
        public bool Clear()
        {
            //this.total = 0;
            this.quantity = 0;
            this.data.Clear();
            return true;
        }
        #endregion

        #region Properties

        //public Int64 Total
        //{
        //    get { GetTotal(); return this.total; }
        //}
        public int Quantity
        {
            get { return this.quantity; }
        }

        public DataTable Data
        {
            get
            {
                updateStt();
                
                //if (!data.Columns.Contains("Total"))
                //{
                //    data.Columns.Add("Total", typeof(Int64));
                //}
                //foreach (DataRow dong in this.data.Rows)
                //{
                //    dong["Total"] = (long)dong["Price"] * (int)dong["Quantity"];
                //}
                return this.data;
            }
        }

        #endregion
    }
}